package homework;

import java.util.ArrayList;

public class HomeWork40 {
    StringBuilder stringBuilder=new StringBuilder();
    public String tree2str(TreeNode root) {
        if(root==null){
            return null;
        }
        stringBuilder.append(root.val);
        if(root.right==null&&root.left==null){
           return stringBuilder.append(")").toString();
        }
        if(root.left!=null&&root.right==null){
            stringBuilder.append(")");
        }
        if(root.left!=null){
            stringBuilder.append("(");
            return tree2str(root.left);
        }
        if(root.right!=null){
            stringBuilder.append("(");
            return tree2str(root.right);
        }
        return stringBuilder.toString();
    }
}
